/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */

package net.stuff.servoy.beans.calendar;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.Serializable;
import java.text.SimpleDateFormat;

import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import net.stuff.servoy.beans.calendar.swing.SwingDateChooser;
import net.stuff.servoy.beans.calendar.wicket.WicketDateChooser;

import com.servoy.j2db.IApplication;
import com.servoy.j2db.IServoyBeanFactory;
import com.servoy.j2db.plugins.IClientPluginAccess;
import com.servoy.j2db.ui.IComponent;

/**
 * A replacement bean for the Servoy Date picker field that 
 * will instanciate either a Swing component of a Wicket component depending on the client type
 * 
 * @author Servoy-stuff
 * http://www.servoy-stuff.net/
 */
public class DateChooserBean implements IServoyBeanFactory, Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private IDateChooserBeanComponent component = new SwingDateChooser();
	private String dataProviderID;
	private String name;
	
	private Color background;
	private Border border;
	private String format = new SimpleDateFormat("yyyy-MM-dd").toPattern();
	private Color foreground;
	private boolean opaque = true;
	private String toolTipText;
	private String titleText;

	private Dimension minimumSize = new Dimension(27,20);
	private Dimension preferredSize = new Dimension(140,20);
	
	private Color validColor;
	private Color errorColor;
	
	private Font font;
	private Point location;
	private Dimension size;
	private boolean editable = true;
	private boolean visible = true;
	private int horizontalAlignment = -1;
	private boolean selectOnEnter;
	
	private String onAction;
	private String onDataChange;
	private String onFocusGained;
	private String onFocusLost;
	
	IClientPluginAccess app;
	
	/* (non-Javadoc)
	 * @see com.servoy.j2db.IServoyBeanFactory#getBeanInstance(int, com.servoy.j2db.plugins.IClientPluginAccess, java.lang.Object[])
	 */
	public IComponent getBeanInstance(int appMode, IClientPluginAccess app, Object[] paramArrayOfObject) {
		if (appMode == IApplication.HEADLESS_CLIENT || appMode == IApplication.WEB_CLIENT) {
			component = createWicketDateChooser(paramArrayOfObject, app);
		}
		component.initialize(app);
		setDataProviderID(dataProviderID);
		setOpaque(opaque);
		if (appMode != IApplication.HEADLESS_CLIENT && appMode != IApplication.WEB_CLIENT) {
			setFormat(format);
			setPreferredSize(preferredSize);
			setMinimumSize(minimumSize);
			((SwingDateChooser)component).setServoyBorder(new EtchedBorder(1));
		} else {
			initWicketComponent();
		}
		return component;
	}
	
	/**
	 * Init the Wicket component
	 */
	private void initWicketComponent() {
		setFont(font);
		setBackground((background == null) ? Color.white : background);
		setForeground((foreground == null) ? Color.black : foreground);
		setValidColor((validColor == null) ? new Color(0, 150, 0) : validColor);
		setErrorColor((errorColor == null) ? Color.red : errorColor);
		setBorder((border == null) ? new EtchedBorder(1) : border);
		setSize(size);
		setLocation(location);
        setHorizontalAlignment(horizontalAlignment);
        setOnAction(onAction);
        setOnDataChange(onDataChange);
        setOnFocusGained(onFocusGained);
        setOnFocusLost(onFocusLost);
        setEditable(editable);
        setSelectOnEnter(selectOnEnter);
        setTitleText(titleText);
        setToolTipText(toolTipText);
        setName(name);
	}
	
	/**
	 * Instanciate the Wicket Component
	 * @param paramArrayOfObject
	 * @param app
	 * @return
	 */
	private WicketDateChooser createWicketDateChooser(
			Object[] paramArrayOfObject, IClientPluginAccess app) {
		WicketDateChooser wicketDateChooser = new WicketDateChooser(paramArrayOfObject[0].toString(), format);
		wicketDateChooser.initialize(app);
		return wicketDateChooser;
		
        
	}

	/*******************************************************************
	 * JavaBeans Properties
	 *******************************************************************/

	/**
	 * @return the dataProviderID
	 */
	public String getDataProviderID() {
		return dataProviderID;
	}
	
	/**
	 * @param s
	 */
	public void setDataProviderID(String s) {
		dataProviderID = s;
		if (component != null)
			component.setDataProviderID(s);
	}
	
	/**
	 * @return the format
	 */
	public String getFormat() {
		return format;
	}
	
	/**
	 * @param s
	 */
	public void setFormat(String s) {
		format = s;
		if (component != null)
			component.setFormat(s);
	}
	
	/**
	 * @return the toolTipText
	 */
	public String getToolTipText() {
		return toolTipText;
	}

	/**
	 * @param s
	 */
	public void setToolTipText(String s) {
		toolTipText = s;
		if (component != null)
			component.setToolTipText(s);
	}

    /* (non-Javadoc)
     * @see com.servoy.j2db.persistence.Zdb#getName()
     */
    public String getName() {
		return name;
    }

    /**
     * @param s
     */
    public void setName(String s) {
        name = s;
        if(component != null)
        	component.setName(s);
    }
    
	
	/**
	 * @return the minimumSize
	 */
	public Dimension getMinimumSize() {
		return minimumSize;
	}
	
	/**
	 * @param min
	 */
	public void setMinimumSize(Dimension min) {
		minimumSize = min;
		if (component != null && component instanceof SwingDateChooser)
			((SwingDateChooser)component).setMinimumSize(min);
	}

	/**
	 * @return the preferredSize
	 */
	public Dimension getPreferredSize() {
		return preferredSize;
	}

	/**
	 * @param p
	 */
	public void setPreferredSize(Dimension p) {
		preferredSize = p;
		if (component != null && component instanceof SwingDateChooser)
			((SwingDateChooser)component).setPreferredSize(p);
	}

    /**
     * @return the border
     */
    public Border getBorder() {
		return border;
    }

    /**
     * @param border
     */
    public void setBorder(Border border) {
        this.border = border;
        if (component != null)
        	component.setBorder(border);
    }

    /**
     * @return the foreground
     */
    public Color getForeground() {
		return foreground;
    }

    /**
     * @param color
     */
    public void setForeground(Color color) {
        foreground = color;
        if (component != null)
        	component.setForeground(color);
    }

    /**
     * @return the background
     */
    public Color getBackground() {
		return background;
    }

    /**
     * @param color
     */
    public void setBackground(Color color) {
        background = color;
        if (component != null)
        	component.setBackground(color);
    }

    /**
     * @return
     */
    public Font getFont() {
		return font;
    }

    /**
     * @param font
     */
    public void setFont(Font font) {
        this.font = font;
        if (component != null)
        	component.setFont(font);
    }

    /**
     * @return the location
     */
    public Point getLocation() {
		return location;
    }

    /**
     * @param point
     */
    public void setLocation(Point point) {
        location = point;
        if (component != null)
        	component.setLocation(point);
    }

    /**
     * @return the size
     */
    public Dimension getSize() {
		return size;
    }

    /**
     * @param dimension
     */
    public void setSize(Dimension dimension) {
        size = dimension;
        if (component != null)
        	component.setSize(dimension);
    }

    /**
     * @param rectangle
     */
    public void setBounds(Rectangle rectangle) {
        setLocation(rectangle.getLocation());
        setSize(rectangle.getSize());
    }

	/**
	 * @return the editable
	 */
	public boolean isEditable() {
		return editable;
	}
	
	/**
	 * @return the editable
	 */
	public boolean getEditable() {
		return isEditable();
	}

	/**
	 * @param editable the editable to set
	 */
	public void setEditable(boolean editable) {
		this.editable = editable;
		if (component != null)
			component.setEditable(editable);
	}

	/**
	 * @return the validColor
	 */
	public Color getValidColor() {
		return validColor;
	}

	/**
	 * @param validColor the validColor to set
	 */
	public void setValidColor(Color validColor) {
		this.validColor = validColor;
		if (component != null)
			component.setValidColor(validColor);
	}

	/**
	 * @return the errorColor
	 */
	public Color getErrorColor() {
		return errorColor;
	}

	/**
	 * @param errorColor the errorColor to set
	 */
	public void setErrorColor(Color errorColor) {
		this.errorColor = errorColor;
		if (component != null)
			component.setErrorColor(errorColor);
	}

	/**
	 * @return the visible
	 */
	public boolean isVisible() {
		return visible;
	}

	/**
	 * @param visible the visible to set
	 */
	public void setVisible(boolean visible) {
		this.visible = visible;
		if (component != null)
			component.setComponentVisible(visible);
	}

	public int getHorizontalAlignment() {
		return horizontalAlignment;
	}

	public void setHorizontalAlignment(int horizontalAlignment) {
		this.horizontalAlignment = horizontalAlignment;
		int horizontal = (horizontalAlignment != SwingConstants.LEFT && horizontalAlignment != SwingConstants.CENTER && horizontalAlignment != SwingConstants.RIGHT) ? SwingConstants.LEFT : horizontalAlignment;
		if (component != null)
			component.setHorizontalAlignment(horizontal);
	}

	/**
	 * @return the onAction
	 */
	public String getOnAction() {
		return onAction;
	}

	/**
	 * @param onAction the onAction to set
	 */
	public void setOnAction(String onAction) {
		this.onAction = onAction;
		if (component != null)
			component.setOnAction(onAction);
	}

	/**
	 * @return the onDataChange
	 */
	public String getOnDataChange() {
		return onDataChange;
	}

	/**
	 * @param onDataChange the onDataChange to set
	 */
	public void setOnDataChange(String onDataChange) {
		this.onDataChange = onDataChange;
		if (component != null)
			component.setOnDataChange(onDataChange);
	}

	/**
	 * @return the onFocusGained
	 */
	public String getOnFocusGained() {
		return onFocusGained;
	}

	/**
	 * @param onFocusGained the onFocusGained to set
	 */
	public void setOnFocusGained(String onFocusGained) {
		this.onFocusGained = onFocusGained;
		if (component != null)
			component.setOnFocusGained(onFocusGained);
	}

	/**
	 * @return the onFocusLost
	 */
	public String getOnFocusLost() {
		return onFocusLost;
	}

	/**
	 * @param onFocusLost the onFocusLost to set
	 */
	public void setOnFocusLost(String onFocusLost) {
		this.onFocusLost = onFocusLost;
		if (component != null)
			component.setOnFocusLost(onFocusLost);
	}

	/**
	 * @return the selectOnEnter
	 */
	public boolean isSelectOnEnter() {
		return selectOnEnter;
	}

	/**
	 * @param selectOnEnter the selectOnEnter to set
	 */
	public void setSelectOnEnter(boolean selectOnEnter) {
		this.selectOnEnter = selectOnEnter;
		if (component != null)
			component.setSelectOnEnter(selectOnEnter);
	}

	/**
	 * @return the titleText
	 */
	public String getTitleText() {
		return titleText;
	}

	/**
	 * @param titleText the titleText to set
	 */
	public void setTitleText(String titleText) {
		this.titleText = titleText;
		if (component != null)
			component.setTitleText(titleText);
	}

	/**
	 * @return the opaque
	 */
	public boolean isOpaque() {
		return this.opaque;
	}

	/**
	 * @param opaque the opaque to set
	 */
	public void setOpaque(boolean opaque) {
		this.opaque = opaque;
		if (component != null)
			component.setOpaque(opaque);
	}

}
